//
//  YHKEmoticonView.swift
//  xinlangweiBo2
//
//  Created by 杨和逵 on 16/3/7.
//  Copyright © 2016年 yanghekui. All rights reserved.
//

import UIKit

//标识
let YHKEmoticonViewReuseIdentifier = "YHKEmoticonViewReuseIdentifier"

class YHKEmoticonView: UIView ,UICollectionViewDataSource,YHKEmoticonToolBarViewDelgate{

 //三部曲
    
  //init方法
   override init(frame: CGRect) {
        super.init(frame: frame)
    
          setupView()
          setupCollectionView()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    //collectionView属性
    private func setupCollectionView(){
        
        //设置代理
        collectionView.dataSource = self
        
        //注册cell
        collectionView.registerClass(YHKEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: YHKEmoticonViewReuseIdentifier)
        
        //改变滚动的方法
        flowLayout.scrollDirection = .Horizontal
        
        //去掉滚动提示
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.showsVerticalScrollIndicator = false
        
        //设置分页
        collectionView.pagingEnabled = true
        
        
    }
    
    
    //布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置item
        flowLayout.itemSize = CGSizeMake(kUIScreenWidth, collectionView.frame.height)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
    }
    
    
    
    

   //创建一个私有方法，添加控件和设置约束
    private func setupView(){
        
        //添加视图
        addSubview(collectionView)
        addSubview(toolBarView)
        
        //添加约束
        collectionView.snp_makeConstraints { (make) -> Void in
            //上 下 左 右 
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(toolBarView.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
        
        
        toolBarView.snp_makeConstraints { (make) -> Void in
            //左 右 下 高
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.height.equalTo(40)
        }
        
        
        
        
    }
    
    //MARK:--懒加载
    //上面的视图
    private lazy var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    private lazy var collectionView:UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.flowLayout)
    
    //下边是自定义的UIView
    private lazy var toolBarView:YHKEmoticonToolBarView = {
    
    let toolBar = YHKEmoticonToolBarView()
        
        //设置代理
        toolBar.delgate = self
        
        return toolBar
    
    }()
}


//MARK:--YHKEmoticonToolBarViewDelgate
extension YHKEmoticonView{
    
    func didToolBarItmeClick(kype: YHKEmoticonToolBarButtonType) {
        
        switch kype {
            
        case .Recent:
            let indexPath = NSIndexPath(forItem: 0, inSection: 0)
            collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
        case .Default:
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
        case .Emoji:
            let indexPath = NSIndexPath(forItem: 0, inSection: 2)
            collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
        case .Lxh:
            let indexPath = NSIndexPath(forItem: 0, inSection: 3)
            collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
        default:break
            
        }
        
        
    }
    
    
}




//MARK--collectionView数据源方法
extension YHKEmoticonView{
    
    //组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return YHKEmoticonManager.getAllEomoticons().count
    }
    
    //行
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return YHKEmoticonManager.getAllEomoticons()[section].count
    }
    
    
    //cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YHKEmoticonViewReuseIdentifier, forIndexPath: indexPath) as! YHKEmoticonCollectionViewCell
        
//        cell.backgroundColor = randomColor()
//        cell.label.text = "第\(indexPath.section)部分 第\(indexPath.row)item"
        
        cell.emoticons = YHKEmoticonManager.getAllEomoticons()[indexPath.section][indexPath.item]
     
        
        return cell
    }
    
    
}
